package is.glif.fenius.translators.example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.ho.yaml.Yaml;

import is.glif.fenius.api.FActivationStatus;
import is.glif.fenius.api.FAvailReq;
import is.glif.fenius.api.FAvailResp;
import is.glif.fenius.api.FCancelReq;
import is.glif.fenius.api.FCancelResp;
import is.glif.fenius.api.FCreateReq;
import is.glif.fenius.api.FCreateResp;
import is.glif.fenius.api.FExternalFault;
import is.glif.fenius.api.FInternalFault;
import is.glif.fenius.api.FListReq;
import is.glif.fenius.api.FListResp;
import is.glif.fenius.api.FQueryReq;
import is.glif.fenius.api.FQueryResp;
import is.glif.fenius.api.FReservation;
import is.glif.fenius.api.FReservationReqParams;
import is.glif.fenius.api.FReservationStatus;
import is.glif.fenius.api.Translator;
import is.glif.fenius.translators.example.bean.ExamplePersistentReservation;
import is.glif.fenius.translators.example.db.ExamplePersistentReservationDAO;
/**
 * a simple translator that mostly returns null
 * @author haniotak
 *
 */
public class StubTranslator implements Translator {
    private final static Logger log = Logger.getLogger(StubTranslator.class);

    public FCancelResp cancel(FCancelReq r) throws FInternalFault,
            FExternalFault {
        return null;
    }

    public FCreateResp create(FCreateReq resrvReq) throws FInternalFault,
            FExternalFault {
        FReservationReqParams rReqParams = resrvReq.getResvParams();
        log.debug("create().start");
        FCreateResp resp = new FCreateResp();
        FReservation resrv = new FReservation();

        resrv.setSrcEndpoint(rReqParams.getSrcEndpoint());
        resrv.setDstEndpoint(rReqParams.getDstEndpoint());
        resrv.setActivationTime(new Date());
        resrv.setDuration(rReqParams.getDuration());
        resrv.setBandwidth(rReqParams.getBandwidth());
        resrv.setSrcVlanTag(rReqParams.getSrcVlanTag());
        resrv.setDstVlanTag(rReqParams.getDstVlanTag());

        /* Here we would call the underlying domain-specific service/control-plane.
         * This is a stub so instead we set the statuses to "FAILED".
         */

        resrv.setReservationStatus(FReservationStatus.FAILED);
        resrv.setActivationStatus(FActivationStatus.FAILED);
        Long now = System.currentTimeMillis()/1000L;
        String resvId = "example-resv-"+now.toString();
        resrv.setReservationId(resvId);
        resp.setResvParams(resrv);

        
        
        // persistence example
        log.debug("starting to save reservation");
        String resvYAML = Yaml.dump(resrv);
        ExamplePersistentReservation prsv = new ExamplePersistentReservation();
        prsv.setResvId(resvId);
        prsv.setResvYAML(resvYAML);
        ExamplePersistentReservationDAO dao = new ExamplePersistentReservationDAO();
        
        List<ExamplePersistentReservation> list = new ArrayList<ExamplePersistentReservation>();
        list.add(prsv);
        dao.create(list);
        log.debug("done saving reservation");
        
        
        
        
        log.debug("create().returning ");

        return resp;
    }

    public FAvailResp isAvailable(FAvailReq r) throws FInternalFault,
            FExternalFault {
        // TODO Auto-generated method stub
        return null;
    }

    public FListResp list(FListReq r) throws FInternalFault, FExternalFault {
        // TODO Auto-generated method stub
        return null;
    }

    public FQueryResp query(FQueryReq r) throws FInternalFault, FExternalFault {
        // TODO Auto-generated method stub
        return null;
    }

}
